package com.forlove.app.manager.async;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.event.ActiveFlagEvent;
import com.forlove.app.event.EventEntity;
import com.forlove.app.manager.EventManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.manager.UserManager;
import com.forlove.app.qselect.index.entity.NewActiveFlagEntity;
import com.forlove.app.qselect.login.entity.UserEntity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

/**
 * 后台更新用户信息
 * Created by yjl on 2017/10/21.
 */

public class UpdateUserinfoAsync extends AsyncTaskLoader<UserEntity> {
    public static final int ASYNC_ID = 20010;

    public UpdateUserinfoAsync(Context context) {
        super(context);
    }

    @Override
    public UserEntity loadInBackground() {
        final UserEntity userEntity = UserManager.getUesrInfo(getContext());
        try {
            Map param = new HashMap();
            param.put("mobile", userEntity.userMobile);
            NetworkManager.getInstance().loadDataByPost(ServerManager.ACTION_PERSON_DETAIL_INFO,
                    param, new ICallBack() {
                        @Override
                        public void onSuccess(int statusCode, String result) {
//                            LogUtil.e("yjl", "更新用户信息:" + result);
                            ParseHelper.getModel(result, "data.user", UserEntity.class);
                            userEntity.parseJSONString(ParseHelper.getString(result, "data.user"));
                            UserManager.saveUserInfo(getContext(), userEntity);
                            EventEntity eventEntity = new EventEntity();
                            eventEntity.eventId = EventManager.EVENT_UPDATE_USERINFO;
                            eventEntity.eventName = "update_user_info";
                            EventBus.getDefault().post(eventEntity);
                        }

                        @Override
                        public void onFail(int statusCode, String result) {


                        }
                    });

            NetworkManager.getInstance().loadDataByPost(ServerManager.ACTION_GET_INFO,
                    new HashMap(), new ICallBack() {
                        @Override
                        public void onSuccess(int statusCode, String result) {
//                            LogUtil.e("yjl", "获取用户最新消息flag:" + result);
                            NewActiveFlagEntity newActiveFlagEntity = new NewActiveFlagEntity();
                            newActiveFlagEntity.parseJSONString(ParseHelper.getString(result, "data"));
                            ActiveFlagEvent eventEntity = new ActiveFlagEvent();
                            eventEntity.eventId = EventManager.EVENT_NEW_FLAG;
                            eventEntity.eventName = "has_news";
                            eventEntity.lastRecord = newActiveFlagEntity.lastRecord;
                            eventEntity.unread = newActiveFlagEntity.unread;
                            eventEntity.newFAddtime = newActiveFlagEntity.newFAddtime;
                            eventEntity.requestCount = newActiveFlagEntity.requestCount;
                            EventBus.getDefault().post(eventEntity);
//                            int oldId = UserManager.getNewCircleInfo(getContext());
//                            if (newActiveFlagEntity.lastRecord > oldId) {
//                                //有新的好友动态
//                                ActiveFlagEvent eventEntity = new ActiveFlagEvent();
//                                eventEntity.eventId = EventManager.EVENT_NEW_CIRCLE_INFO;
//                                eventEntity.eventName = "get_news";
//                                eventEntity.lastRecord = newActiveFlagEntity.lastRecord;
//                                eventEntity.unread = newActiveFlagEntity.unread;
//                                eventEntity.newFAddtime = newActiveFlagEntity.newFAddtime;
//                                EventBus.getDefault().post(eventEntity);
//                            }
//                            long oldAddtime = FriendManager.getNewFriendAddTime(getContext());
//                            if (newActiveFlagEntity.newFAddtime > oldAddtime) {
//                                //有新的好友添加
//                                ActiveFlagEvent eventEntity = new ActiveFlagEvent();
//                                eventEntity.eventId = EventManager.EVENT_NEW_FRIEND;
//                                eventEntity.eventName = "has_new_friend";
//                                eventEntity.lastRecord = newActiveFlagEntity.lastRecord;
//                                eventEntity.unread = newActiveFlagEntity.unread;
//                                eventEntity.newFAddtime = newActiveFlagEntity.newFAddtime;
//                                EventBus.getDefault().post(eventEntity);
//                            }

                        }

                        @Override
                        public void onFail(int statusCode, String result) {

                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userEntity;
    }

    @Override
    protected void onStartLoading() {
        super.onStartLoading();
        forceLoad();
    }

}
